<template>
  <div class="front">
    <div class="blogitem">
      <el-card class="itemCard">
        <el-row type="flex"
                justify="center">
          <el-col :span="22"
                  align="middle">

            <el-form :model="form"
                     :rules="rules"
                     ref="form">
              <el-form-item label="文章标题"
                            prop="title">
                <el-input v-model="form.title"
                          :readonly="lodding"></el-input>
              </el-form-item>
              <el-form-item label="文章描述"
                            prop="description">
                <el-input type="textarea"
                          :rows="2"
                          placeholder="请输入内容"
                          v-model="form.description"
                          :readonly="lodding"></el-input>
              </el-form-item>
              <el-form-item prop="content">
                <mavon-editor v-model="form.content"
                              @imgAdd="$imgAdd"
                              @imgDel="$imgDel"
                              style="max-height:620px;min-height:620px" />
              </el-form-item>
            </el-form>
            <el-button type="success"
                       @click="submitForm">发布文章</el-button>
          </el-col>
        </el-row>

      </el-card>

    </div>

  </div>
</template>
<script>
export default {
  name: '',
  components: {},
  props: {},
  data() {
    return {
      lodding: false,
      form: {
        id: '',
        title: '',
        description: '',
        content: '',
      },
      rules: {
        title: [
          { required: true, message: '标题不能为空', trigger: 'blur' },
          {
            max: 255,
            message: '不能超过255个字符',
            trigger: 'blur',
          },
        ],
        description: [
          { required: true, message: '描述不能为空', trigger: 'blur' },
          {
            max: 20,
            message: '不能超过255个字符',
            trigger: 'blur',
          },
        ],
        content: [{ required: true, message: '内容不能为空', trigger: 'blur' }],
      },
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      this.clear()
      this.lodding = false
      this.form.id = this.$route.params.blogId
      if (this.form.id) {
        this.$axios
          .get('/blog/detail?id=' + this.form.id, {
            headers: {
              Authorization: localStorage.getItem('Authorization'),
            },
          })
          .then((res) => {
            this.form = res.data.data
          })
      }
    },
    clear() {
      this.form.id = ''
      this.form.title = ''
      this.form.description = ''
      this.form.content = ''
    },
    // 绑定@imgAdd event
    $imgAdd(pos, $file) {
      // 第一步.将图片上传到服务器.
      var formdata = new FormData()
      formdata.append('image', $file)
      this.$axios({
        url: '/blog/uploadImg',
        method: 'post',
        data: formdata,
        headers: { 'Content-Type': 'multipart/form-data' },
      }).then((res) => {
        let url = res.data.data.imgUrl
        let name = $file.name
        console.log('url', res.data.data.imgUrl)
        console.log('name', name)
        if (name.includes('-')) {
          name = name.replace(/-/g, '')
        }
        let content = this.form.content
        // 第二步.将返回的url替换到文本原位置![...](0) -> ![...](url)  这里是必须要有的
        if (content.includes(name)) {
          let oStr = `(${pos})`
          let nStr = `(${url})`
          let index = content.indexOf(oStr)
          let str = content.replace(oStr, '')
          let insertStr = (soure, start, newStr) => {
            return soure.slice(0, start) + newStr + soure.slice(start)
          }
          this.form.content = insertStr(str, index, nStr)
        }
      })
    },
    $imgDel(pos, $file) {},
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.lodding = false
          this.$axios
            .put('/blog/addOrEdit', this.form, {
              headers: {
                Authorization: localStorage.getItem('Authorization'),
              },
            })
            .then((res) => {
              this.$message({
                type: 'success',
                message: `发表成功`,
              })
              this.$router.push('/')
            })
        }
      })
    },
  },
  watch: {},
  computed: {},
  mounted() {},
}
</script>
<style scoped>
</style>